package cn.jingzhuan.element.demo.view.activity

import android.content.Context
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.LinearSnapHelper
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.GridView
import android.widget.Toast
import cn.jingzhuan.element.demo.view.BaseBindingActivity
import cn.jingzhuan.element.demo.view.adapter.BaseRecyclerAdapter
import cn.jingzhuan.element.demo.view.adapter.PageRecyclerAdapter
import cn.jingzhuan.element.demo.view.adapter.RecyclerViewHolder
import cn.jingzhuan.element.demo.view.model.ItemDescription
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.ActivityFeatureLayoutBinding
import cn.jingzhuan.element.element.databinding.ItemDescriptionBinding


class FeatureLayoutActivity : BaseBindingActivity<ActivityFeatureLayoutBinding>(){
    override fun getLayoutId(): Int  = R.layout.activity_feature_layout

    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityFeatureLayoutBinding) {
        binding.viewToolbar?.title = intent.getStringExtra("title")
        setUpActionBar(binding.viewToolbar!!.toolbar)
        binding.recyclerView.layoutManager = LinearLayoutManager(baseContext, RecyclerView.HORIZONTAL,false)
        var snapHelper = LinearSnapHelper()
        snapHelper.attachToRecyclerView(binding.recyclerView)

        var index = 0
        var mutableList = mutableListOf<ItemDescription>()
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.drawable.ico_back))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.ic_launcher))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))
        mutableList.add(ItemDescription(FeatureLayoutActivity::class.java,"list${index++}",R.mipmap.icon_tabbar_component))



        var itemAdapter  = object : PageRecyclerAdapter<ItemDescription>(baseContext,mutableList,6) {

            // 点击事件
            override fun onItemClickListener(datas: MutableList<ItemDescription>?): AdapterView.OnItemClickListener = AdapterView.OnItemClickListener { _parent, _view, _position, _id ->
                Toast.makeText(_parent.context,"正在点击:${datas?.get(_position)?.name}",Toast.LENGTH_SHORT).show()
            }

            // 数据绑定
            override fun bindData(data: ItemDescription, convertView: View?, parent: ViewGroup?): View {
                var binding : ItemDescriptionBinding
                var convertView = convertView
                if(convertView == null) {
                    binding = DataBindingUtil.inflate(LayoutInflater.from(parent?.context), R.layout.item_description,parent,false)
                    convertView = binding.root
                    convertView.tag = binding

                } else{
                    binding = convertView.tag as ItemDescriptionBinding
                }
                binding.item = data
                return convertView
            }

            // 上层容器
            override fun getRootLayoutId(viewType: Int): Int = R.layout.layout_page_grid

        }

        binding.recyclerView.adapter = itemAdapter

        // 页码显示
        binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {

                val position = recyclerView?.layoutManager?.getPosition(snapHelper.findSnapView(
                        recyclerView.layoutManager))
                binding.tvPage.text = "${position?.plus(1)}/${recyclerView?.adapter?.itemCount}"
            }
        })
    }

}



